using ASP;
using HoWave.Common;
using HoWave.Data;
using HoWave.Data.Bll;
using HoWave.Data.Model;
using HoWave.Web;
using HoWave.Web.Shop.Member;
using System;
using System.Data;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public class Member_StockManage_StockAdd : Member, IRequiresSessionState
{
	private string stockInOut = "";
	private string action = "";
	private int infoOrderID = 0;
	private Shop_Product _shop_product = null;
	public string strID = "";
	public string strIDStock = "";
	public string strStockStatue = "";
	protected ScriptManager ScriptManager1;
	protected Literal ltrTitle;
	protected TextBox txtStockNumber;
	protected TextBox txtInputTime;
	protected TextBox txtRemarks;
	protected Label lblGoodsTotal;
	protected HiddenField hfProductID;
	protected HiddenField hfProductStr;
	protected HiddenField hfMoneyGoods;
	protected HtmlGenericControl divProduct;
	protected CheckBox MessageStatus;
	protected CheckBox EmailStatus;
	protected CheckBox MobileStatus;
	protected HtmlGenericControl divNotice;
	protected HiddenField hfStockStatus;
	protected Button btnSave;
	protected Button btnCancle;
	public string StockInOut
	{
		get
		{
			if (base.Request.QueryString["stockType"] != null)
			{
				this.stockInOut = base.Request.QueryString["stockType"].Trim();
			}
			else
			{
				this.stockInOut = "";
			}
			return this.stockInOut;
		}
		set
		{
			this.stockInOut = value;
		}
	}
	public string Action
	{
		get
		{
			if (base.Request.QueryString["Action"] != null)
			{
				this.action = base.Request.QueryString["Action"].Trim();
			}
			else
			{
				this.action = "";
			}
			return this.action;
		}
		set
		{
			this.action = value;
		}
	}
	public int InfoOrderID
	{
		get
		{
			if (base.Request.QueryString["infoOrderID"] != null)
			{
				this.infoOrderID = Convert.ToInt32(base.Request.QueryString["infoOrderID"].Trim());
			}
			return this.infoOrderID;
		}
		set
		{
			this.infoOrderID = value;
		}
	}
	protected Shop_Product shop_product
	{
		get
		{
			Shop_Product shop_product;
			if (this._shop_product == null)
			{
				this._shop_product = new Shop_Product(this.conn);
				shop_product = this._shop_product;
			}
			else
			{
				shop_product = this._shop_product;
			}
			return shop_product;
		}
	}
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!base.IsPostBack)
		{
			if (!base.OwnerStockPower(base.user.UserID))
			{
				base.WriteRightErrMsg();
			}
			string text = this.Action;
			if (text != null)
			{
				if (!(text == "addinfo"))
				{
					if (text == "editinfo")
					{
						if (this.StockInOut == "inStock")
						{
							this.ltrTitle.Text = "修改入库单";
						}
						else
						{
							if (this.StockInOut == "outStock")
							{
								this.ltrTitle.Text = "修改出库单";
							}
						}
					}
				}
				else
				{
					if (this.StockInOut == "inStock")
					{
						this.txtStockNumber.Text = "RK" + DateTime.Now.TimeString();
						this.ltrTitle.Text = "添加入库单";
					}
					else
					{
						if (this.StockInOut == "outStock")
						{
							this.txtStockNumber.Text = "CK" + DateTime.Now.TimeString();
							this.ltrTitle.Text = "添加出库单";
						}
					}
				}
			}
			if (this.InfoOrderID > 0)
			{
				this.InitPage();
			}
		}
	}
	private bool CheckInput()
	{
		bool result = true;
		if (!string.IsNullOrEmpty(this.txtInputTime.Text))
		{
			if (!PageValidate.IsDateTime(this.txtInputTime.Text))
			{
				MessageBox.Show(this, "记录时间格式错误！");
				result = false;
			}
		}
		return result;
	}
	protected void InitPage()
	{
		HoWave.Data.Model.StockInfoOrder model = BLLInstance.BllStockInfoOrder.GetModel(this.InfoOrderID);
		if (model != null)
		{
			this.txtStockNumber.Text = model.InfoNumber;
			this.txtInputTime.Text = model.CreateDate.ToString();
			this.txtInputTime.Enabled = false;
			this.txtRemarks.Text = model.Remark;
			this.strStockStatue = model.StockStatus.ToString();
			DataTable stockOrderItemByInfoOrderID = this.GetStockOrderItemByInfoOrderID(model.InfoOrderID);
			for (int i = 0; i < stockOrderItemByInfoOrderID.Rows.Count; i++)
			{
				this.strID = this.strID + stockOrderItemByInfoOrderID.Rows[i]["ProcuctID"].ToString() + ",";
				string text = this.strIDStock;
				this.strIDStock = string.Concat(new string[]
				{
					text,
					stockOrderItemByInfoOrderID.Rows[i]["ProcuctID"].ToString(),
					",",
					stockOrderItemByInfoOrderID.Rows[i]["StockCount"].ToString(),
					"|"
				});
			}
			if (this.strID != "" && this.strIDStock != "")
			{
				this.strID = this.strID.Substring(0, this.strID.Length - 1);
				this.strIDStock = this.strIDStock.Substring(0, this.strIDStock.Length - 1);
			}
		}
	}
	private HoWave.Data.Model.StockInfoOrder GetStockInfo()
	{
		HoWave.Data.Model.StockInfoOrder stockInfoOrder = new HoWave.Data.Model.StockInfoOrder();
		string text = this.txtStockNumber.Text;
		DateTime value = DateTime.Parse(this.txtInputTime.Text);
		string text2 = this.txtRemarks.Text;
		string stockOrderType = "";
		int value2 = 0;
		if (this.StockInOut == "inStock")
		{
			stockOrderType = "inPurchase";
			value2 = 1;
			stockInfoOrder.StockDate = new DateTime?(DateTime.Now);
		}
		else
		{
			if (this.StockInOut == "outStock")
			{
				stockOrderType = "outSell";
				value2 = 3;
				stockInfoOrder.StockDate = new DateTime?(DateTime.Now);
			}
		}
		decimal value3 = 0m;
		if (!string.IsNullOrEmpty(this.hfMoneyGoods.Value))
		{
			value3 = Convert.ToDecimal(this.hfMoneyGoods.Value);
		}
		this.hfStockStatus.Value = value2.ToString();
		stockInfoOrder.InfoNumber = text;
		stockInfoOrder.CreateDate = new DateTime?(value);
		stockInfoOrder.Remark = text2;
		stockInfoOrder.StockOrderType = stockOrderType;
		stockInfoOrder.StockStatus = new int?(value2);
		stockInfoOrder.Inputer = base.user.UserName;
		stockInfoOrder.OwnerID = base.user.UserID;
		stockInfoOrder.TotalMoney = new decimal?(value3);
		return stockInfoOrder;
	}
	private DataTable GetStockOrderItemByInfoOrderID(int infoid)
	{
		string strWhere = "InfoOrderID =" + infoid;
		return BLLInstance.BllStockItemOrder.GetList(strWhere).Tables[0];
	}
	private void UpdateProductCount(int infoID)
	{
		if (infoID > 0)
		{
			DataTable stockOrderItemByInfoOrderID = this.GetStockOrderItemByInfoOrderID(infoID);
			for (int i = 0; i < stockOrderItemByInfoOrderID.Rows.Count; i++)
			{
				int num = Convert.ToInt32(stockOrderItemByInfoOrderID.Rows[i]["ProductID"].ToString());
				int num2 = int.Parse(stockOrderItemByInfoOrderID.Rows[i]["StockCount"].ToString());
				string sql = string.Concat(new object[]
				{
					"update HLShop_Product set Stocks=Stocks+",
					num2,
					" where ProductID=",
					num
				});
				this.conn.Execute(sql);
			}
		}
	}
	protected void btnSave_Click(object sender, EventArgs e)
	{
		if (this.CheckInput())
		{
			string comeUrl = "StockInfoInRecord.aspx";
			HoWave.Data.Model.StockInfoOrder stockInfo = this.GetStockInfo();
			string text = this.Action;
			if (text != null)
			{
				if (!(text == "addinfo"))
				{
					if (!(text == "editinfo"))
					{
					}
				}
				else
				{
					int num = BLLInstance.BllStockInfoOrder.Add(stockInfo);
					if (num > 0)
					{
						int num2 = BLLInstance.BllStockItemOrder.AddStockOrderItem(num, this.txtStockNumber.Text, this.hfProductStr.Value);
						if (num2 > 0)
						{
							if (this.StockInOut == "inStock")
							{
								if (this.hfStockStatus.Value == "1")
								{
									this.UpdateProductCount(num);
								}
							}
							this.WriteSuccessMsg("", "添加信息成功！", comeUrl);
						}
					}
					else
					{
						this.WriteErrMsg("", "添加库信息失败！", "");
					}
				}
			}
		}
	}
	public void btnCancle_Click(object sender, EventArgs e)
	{
		if (this.StockInOut == "inStock")
		{
			base.Response.Redirect("StockInfoInRecord.aspx");
		}
		else
		{
			if (this.StockInOut == "outStock")
			{
				base.Response.Redirect("StockInfoOut.aspx");
			}
		}
	}
	private void SendInfo(int productid)
	{
		string value = "";
		if (!string.IsNullOrEmpty(value))
		{
			string sql = "select * from HL_OutStockLog where NoticeStatue=0 and ProductID=" + productid;
			DataTable dataTable = this.conn.GetDataTable(sql);
			for (int i = 0; i < dataTable.Rows.Count; i++)
			{
				int outStockLogID = Convert.ToInt32(dataTable.Rows[i]["OutStockLogID"].ToString());
				HoWave.Data.Model.OutStockLog model = BLLInstance.BllOutStockLog.GetModel(outStockLogID);
				if (model != null)
				{
					string smtpserver = this.config["MailSmtpServer"].ToString();
					string mailuser = this.config["MailServerUserName"].ToString();
					string mailpassword = this.config["MailServerPassWord"].ToString();
					string mailFrom = this.config["MailServerFrom"].ToString();
					string sql2 = "select ProductName from HLShop_Product where ProductID=" + model.ProductID;
					DataTable dataTable2 = this.conn.GetDataTable(sql2);
					string str = "";
					if (dataTable2.Rows.Count > 0)
					{
						str = dataTable2.Rows[0]["ProductName"].ToString();
					}
					string sql3 = "select UserName from HL_User where UserID=" + model.UserID;
					DataTable dataTable3 = this.conn.GetDataTable(sql3);
					string text = "";
					if (dataTable3.Rows.Count > 0)
					{
						text = dataTable3.Rows[0]["UserName"].ToString();
					}
					string text2 = "到货通知";
					string text3 = text + "您好：您关注的商品：" + str + "已经到货。";
					int noticeStatue = 0;
					if (this.MessageStatus.Checked)
					{
						noticeStatue = 1;
						model.MessageStatus = true;
						string userName = base.user.UserName;
						MessageBll messageBll = new MessageBll(this.conn);
						bool flag = messageBll.AddMessage(userName, "Users", text2, text3, 0, 0, 1, 0, text, "", 1);
					}
					if (this.EmailStatus.Checked)
					{
						string email = model.Email;
						if (!string.IsNullOrEmpty(email))
						{
							noticeStatue = 1;
							model.EmailStatus = true;
							Email email2 = new Email();
							bool flag2 = email2.SendEmail(smtpserver, mailuser, mailpassword, mailFrom, text3, email, text2);
						}
					}
					if (this.MobileStatus.Checked)
					{
						string mobilePhone = model.MobilePhone;
						if (!string.IsNullOrEmpty(mobilePhone))
						{
							noticeStatue = 1;
							model.MobileStatus = true;
							MobileMessage mobileMessage = new MobileMessage();
							bool flag3 = mobileMessage.Send(mobilePhone, text3, text);
						}
					}
					model.NoticeDate = new DateTime?(DateTime.Now);
					model.NoticeStatue = noticeStatue;
					model.OutStockLogID = outStockLogID;
					BLLInstance.BllOutStockLog.Update(model);
				}
			}
		}
	}
}
